package com.catemanagement.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Date;
import java.util.List;

import com.catemanagement.model.Bill;
import com.catemanagement.model.BillMenu;

public class PrintUtil {

    public static void print(PrintWriter printWriter, Bill bill) {
        fillData(printWriter, bill);
    }

    private static void getPrinterState(Socket client) throws IOException, InterruptedException {
        PrintWriter socketWriter;
        socketWriter = new PrintWriter(client.getOutputStream());
        InputStream is = client.getInputStream();
        socketWriter.write(0x1b);
        socketWriter.write(0x76);
        socketWriter.flush();

        Thread.sleep(500);
        int i = is.available();
        while (i-- > 0) {
            System.out.println(is.read());
        }
        client.shutdownInput();
        client.close();
    }

    private static void fillData(PrintWriter socketWriter, Bill bill) {
        fillHeader(socketWriter, bill);
        fillContent(socketWriter, bill);
    }

    private static void fillHeader(PrintWriter socketWriter, Bill bill) {
        socketWriter.write(0x1b);
        socketWriter.write(0x24);
        socketWriter.write(0);
        socketWriter.write(0);
        /* 横向纵向都放大一倍 */
        socketWriter.write(0x1c);
        socketWriter.write(0x21);
        socketWriter.write(12);
        /* 居中 */
        socketWriter.write(0x1b);
        socketWriter.write(0x61);
        socketWriter.write(1);
        /* 标题 */
        socketWriter.println("即起");
        /* 换行 */
        socketWriter.println(" ");
    }

    private static void fillContent(PrintWriter socketWriter, Bill bill) {
        /* 正常字体 */
        socketWriter.write(0x1c);
        socketWriter.write(0x21);
        socketWriter.write(0);
        /* 左对齐 */
        socketWriter.write(0x1b);
        socketWriter.write(0x61);
        socketWriter.write(0);
        /* 正文 */
        socketWriter.println("台    号: " + bill.getTableName());
        // socketWriter.println("入 单 人: " + bill.getSendBy());
        socketWriter.println("入单时间: " + FormatUtil.formatDate(bill.getSendTime(), FormatUtil.TIME));
        socketWriter.println("--------------------------------");
        socketWriter.print("品名");
        socketWriter.write(0x1b);
        socketWriter.write(0x24);
        socketWriter.write(1);
        socketWriter.write(1);
        socketWriter.println("数量  单位");
        socketWriter.println("--------------------------------");
        List<BillMenu> billMenuList = bill.getBillMenuList();
        if (billMenuList != null) {
            for (BillMenu billMenu : billMenuList) {
                socketWriter.print(billMenu.getName());
                socketWriter.write(0x1b);
                socketWriter.write(0x24);
                socketWriter.write(1);
                socketWriter.write(1);
                socketWriter.print(billMenu.getCount());
                socketWriter.write(0x1b);
                socketWriter.write(0x5c);
                socketWriter.write(35);
                socketWriter.write(0);
                socketWriter.println(billMenu.getUnit());
            }
        }
        socketWriter.println("--------------------------------");
        socketWriter.println("打印时间: " + FormatUtil.formatDate(new Date(), FormatUtil.DATE_TIME));

        for (int i = 0; i < 3; i++) {
            socketWriter.println(" ");
        }
    }

}
